package repository

import (
	"context"
	"strconv"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
	"github.com/jmoiron/sqlx"
	"go.uber.org/zap"
)

// MysqlPaymentMethodRepository is data/repository implementation
// of service layer MysqlPaymentMethodRepository
type MysqlPaymentMethodRepository struct {
	DB *sqlx.DB
}

// NewPaymentMethodRepository is a factory for initializing Employee Repository
func NewPaymentMethodRepository(db *sqlx.DB) model.PaymentMethodRepository {
	return &MysqlPaymentMethodRepository{
		DB: db,
	}
}

// Create is to Create payment_methods in mysql
func (m *MysqlPaymentMethodRepository) Create(ctx context.Context, p *model.PaymentMethod) error {
	query := `INSERT INTO payment_methods(employee_id) VALUES(?)`
	if _, err := m.DB.Exec(query, p.EmployeeID); err != nil {
		zap.L().Error("repository cound not create a payment_methods with err: ", zap.Error(err))
		return apperrors.NewInternal()
	}

	return nil
}

// Find PaymentClassification by Employee ID
func (m *MysqlPaymentMethodRepository) FindByEmlpoyeeID(ctx context.Context, id int64) (*model.PaymentMethod, error) {
	paymentMethod := &model.PaymentMethod{}

	query := `SELECT * FROM payment_methods WHERE employee_id = ?`

	// we need to actually check errors as it could be something other than not found
	if err := m.DB.Get(paymentMethod, query, id); err != nil {
		zap.L().Error("repository cound not get payment_classifications with err: ", zap.Error(err))
		return paymentMethod, apperrors.NewNotFound("id", strconv.Itoa(int(id)))
	}

	return paymentMethod, nil
}
